#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

struct image {
    int recordMan[1024];
    int recordWomen[1024];
    vector<int> men, women;
    int K;
};

bool people[9999];
double ca[9999], cb[9999], caMax = 0, cbMax = 0;

vector<image> images;

bool cmp(pair<int, int> a, pair<int, int> b) {
    return a.second < b.second;
}

void searchABsCP(string A, string B) {
    int A1 = abs(atoi(A.c_str())), B1 = abs(atoi(B.c_str()));
    for(auto i: images) {
        if(i.recordMan[A1] == 1 || i.recordWomen[A1] == 1) {
            if(people[A1] == false) {
                /* A1 is F */
                for(auto j: i.men) {
                    ca[j] += 1.0/i.K;
                    if(ca[j] > caMax) caMax = ca[j];
                }
            } else {
                for(auto j: i.women) {
                    ca[j] += 1.0/i.K;
                    if(ca[j] > caMax) caMax = ca[j];
                }
            }
        }
        if(i.recordMan[B1] == 1 || i.recordWomen[B1] == 1) {
            if(people[B1] == false) {
                /* A1 is F */
                for(auto j: i.men) {
                    cb[j] += 1.0/i.K;
                    if(cb[j] > cbMax) cbMax = cb[j];
                }
            } else {
                for(auto j: i.women) {
                    cb[j] += 1.0/i.K;
                    if(cb[j] > cbMax) cbMax = cb[j];
                }
            }
        }
    }
}

void getRes(string A, string B, int N) {
    vector<pair<int, int>> res1, res2;
    int A1 = abs(atoi(A.c_str())), B1 = abs(atoi(B.c_str()));
    if(cb[A1] == cbMax && ca[B1] == caMax) {
        cout << A << " " << B << endl;
    } else {
        for(int i = 0; i < N; ++i) {
            if(ca[i] == caMax) {
                res1.push_back({A1, i});
            }
        }
        for(int i = 0; i < N; ++i) {
            if(cb[i] == cbMax) {
                res2.push_back({B1, i});
            }
        }
        sort(res1.begin(), res1.end(), cmp);
        sort(res2.begin(), res2.end(), cmp);
        for(auto i:res1) {
            cout << A << " ";
            if(people[i.second] == false) {
                cout << "-" << i.second;
            } else {
                cout << i.second;
            }
            cout << endl;
        }
        for(auto i:res2) {
            cout << B << " ";
            if(people[i.second] == false) {
                cout << "-" << i.second;
            } else {
                cout << i.second;
            }
            cout << endl;
        }
    }
}

void init(image *temp, int N) {
    for(int i = 0; i < N; ++i) {
        temp->recordMan[i] = temp->recordWomen[i] = 0;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int N, M, K;
    string s, A, B;
    cin >> N >> M;
    for(int i = 1; i <= M; ++i) {
        image temp;
        init(&temp, N);
        cin >> K;
        temp.K = K;
        for(int i = 1; i <= K; ++i) {
            cin >> s;
            if(s[0] == '-') {
                people[abs(atoi(s.c_str()))] = false;
                temp.women.push_back(abs(atoi(s.c_str())));
                temp.recordWomen[abs(atoi(s.c_str()))] = 1;
            } else {
                people[abs(atoi(s.c_str()))] = true;
                temp.men.push_back(abs(atoi(s.c_str())));
                temp.recordMan[abs(atoi(s.c_str()))] = 1;
            }
        }
        images.push_back(temp);
    }
    cin >> A >> B;
    searchABsCP(A, B);
    getRes(A, B, N);
}
